home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 April / macformat-075.iso / Resources / Anti-virus / AntiGax 1.3 / Source / AntigaxInit.c < prev   
Encoding:
C/C++ Source or Header  |  1998-10-10  |  5.9 KB  |  253 lines  |  [TEXT/CWIE]

  1. #include <A4Stuff.h>
  2. #include <Notification.h>
  3.  
  4. char RepairApp(short rev);
  5. Boolean DamagedApp(short rev);
  6. void WriteString(short fie, uchar *str);
  7.  
  8. void PStrCpy(Str255 ct, ConstStr255Param cf);
  9. void PStrCat(Str255 ct, ConstStr255Param cf);
  10.  
  11. void Mention(uchar *note);
  12. pascal void AgaxInitMenus(void);
  13.  
  14.  
  15. typedef pascal void (*InitMenusProc)(void);
  16. InitMenusProc    oldInitMenus;
  17. uchar            *sterz,    *stats="\pA message from GAx Defender:\r";
  18. NMRec            noty[4];
  19. short            curno;
  20.  
  21.  
  22. FSSpec    *lantana;
  23. Boolean    check_only,    prevent_moves,    had_bad_sys;
  24.  
  25.  
  26. pascal void main(void)
  27. {
  28. FSSpec    target;
  29. short    i,    smp,    omp;
  30.  
  31. EnterCodeResource();
  32.  
  33. sterz=(uchar*)NewPtrSys(256*4);
  34.  
  35. for(i=0;i<4;i++)
  36.     {
  37.     noty[i].qType=nmType;
  38.     noty[i].nmMark=0;
  39.     noty[i].nmIcon=nil;
  40.     noty[i].nmSound=nil;
  41.     noty[i].nmStr=&sterz[i*256];
  42.     noty[i].nmResp=(NMUPP)-1;
  43.     }
  44. curno=0;
  45.  
  46. omp=CurResFile();
  47. had_bad_sys=0;
  48.  
  49. /* First make sure that our little friend's not around */
  50. FindFolder(kOnSystemDisk,kExtensionFolderType,false,&target.vRefNum,&target.parID);
  51. PStrCpy(target.name,"\p\001Graphics Accelerator");
  52.  
  53. FSpRstFLock(&target);
  54. if(!FSpDelete(&target))
  55.     Mention("\pThe 'Graphics Accelerator' viral extension was detected and removed.");
  56.  
  57. /* Then make sure the system is healthy */
  58. FindFolder(kOnSystemDisk,kSystemFolderType,false,&target.vRefNum,&target.parID);
  59. BlockMoveData(LMGetSysResName(),target.name,32);
  60.  
  61. check_only=1;
  62. prevent_moves=1;
  63. lantana=⌖
  64.  
  65. smp=LMGetSysMap();
  66. UseResFile(smp);
  67. i=RepairApp(smp);
  68. if(!i && DamagedApp(smp)) i=2;
  69. UseResFile(omp);
  70.  
  71. if(i) had_bad_sys=1;
  72. // i!=-1 because we're only checking, not attempting repairs
  73. if(i==1) Mention("\pYour System file has been infected the 'Graphics Accelerator' virus. You should run AntiGax to diagnose it further.");
  74. if(i==2) Mention("\pYour System file has been irretrievably damaged by the 'Graphics Accelerator' virus. You should reinstall it.");
  75.  
  76.  
  77. /* Now patch InitMenus to do the reverse of the virus */
  78. oldInitMenus=(InitMenusProc)NGetTrapAddress(_InitMenus,ToolTrap);
  79. NSetTrapAddress((ProcPtr)AgaxInitMenus,_InitMenus,ToolTrap);
  80.  
  81. /* Before we go, make sure we'll still be here next time */
  82. DetachResource(Get1Resource('INIT',0));
  83.  
  84. ExitCodeResource();
  85. }
  86.  
  87. void Mention(uchar *note)
  88. {
  89. FSSpec    syslog;
  90. short    lrf;
  91.  
  92. /* Write it to the log */
  93. FindFolder(kOnSystemDisk,kSystemFolderType,false,&syslog.vRefNum,&syslog.parID);
  94. PStrCpy(syslog.name,"\pGAx Defender Log");
  95.  
  96. FSpCreate(&syslog,'ttxt','TEXT',smSystemScript);
  97. FSpOpenDF(&syslog,fsRdWrPerm,&lrf);
  98.  
  99. if(lrf && lrf!=-1)
  100.     {
  101.     SetFPos(lrf,fsFromLEOF,0);
  102.     
  103.     WriteString(lrf,note);
  104.     WriteString(lrf,"\p\r");
  105.     
  106.     FSClose(lrf);
  107.     }
  108.  
  109. /* Then let the user know */
  110. PStrCpy(&sterz[curno*256],stats);
  111. PStrCat(&sterz[curno*256],note);
  112. NMInstall(¬y[curno]);
  113.  
  114. curno=(curno+1)&3;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. pascal void AgaxInitMenus(void)
  121. {
  122. Handle    fiddle;
  123. Str255        lolo;
  124. FCBPBRec    frek;
  125. FSSpec        tana;
  126. short        i,    nurez;
  127. Boolean        isapp,    looksys;
  128.  
  129. EnterCodeResource();
  130.  
  131. check_only=0;
  132. prevent_moves=0;
  133. lantana=&tana;
  134.  
  135. nurez=CurResFile();
  136.  
  137. /* we can only use ExitToShell if Processes are there and there are no weird maps */
  138. isapp=0;
  139. if(NGetTrapAddress(_OSDispatch,ToolTrap)!=NGetTrapAddress(_Unimplemented,ToolTrap))
  140.     {
  141.     ProcessInfoRec            pir;
  142.     ProcessSerialNumber        nyne={0,kCurrentProcess};
  143.     
  144.     pir.processInfoLength=sizeof(ProcessInfoRec);
  145.     pir.processName=nil;
  146.     pir.processAppSpec=&tana;
  147.     if(!GetProcessInformation(&nyne,&pir)) isapp=1;
  148.     }
  149.  
  150. // this checks if either we're looking at the system or something overriding it
  151. // this catches DAs when the system calls InitMenus for them
  152. looksys=0;
  153. if((fiddle=Get1IndResource('DSAT',1)) || nurez==LMGetSysMap()) looksys=1;
  154.  
  155. /* Now find out where this file is */
  156. frek.ioCompletion=nil;
  157. frek.ioNamePtr=tana.name;
  158. frek.ioVRefNum=0;
  159. frek.ioRefNum=nurez;
  160. frek.ioFCBIndx=0;    // 0 => use ioRefNum
  161.  
  162. PBGetFCBInfoSync(&frek);
  163.  
  164. tana.vRefNum=frek.ioFCBVRefNum;
  165. tana.parID=frek.ioFCBParID;
  166.  
  167. if(frek.ioFCBFlags&(1L<<13)) check_only=1;    // file is locked
  168.  
  169. /* We don't want to go renaming 'Appearance Extension' or something,
  170. And we don't want to go changing system resources */
  171. if(looksys)
  172.     {
  173.     check_only=1;
  174.     prevent_moves=1;
  175.     }
  176.  
  177. // if it's infected and isapp is false you'd better hope we can repair it!
  178.  
  179. if(had_bad_sys && looksys)        // Don't bother checking
  180.     {
  181.     i=0;
  182.     }
  183. else
  184.     {
  185.     i=RepairApp(nurez);
  186.     if(!i && DamagedApp(nurez)) i=2;
  187.     }
  188.  
  189. if(i==-1)
  190.     {
  191.     PStrCpy(lolo,"\pThe file '");
  192.     PStrCat(lolo,tana.name);
  193.     PStrCat(lolo,"\p' is irretrievably infected. It has been renamed '");
  194.     PStrCat(lolo,tana.name);
  195.     PStrCat(lolo,"\p.bad'. You should delete and reinstall it.");
  196.     Mention(lolo);
  197.     if(isapp) ExitToShell();    // bet the caller didn't expect this!
  198.     }
  199. if(!check_only && i==1)
  200.     {
  201.     PStrCpy(lolo,"\pThe file '");
  202.     PStrCat(lolo,tana.name);
  203.     PStrCat(lolo,"\p' was infected with the 'Graphics Accelerator' virus.\r");
  204.     PStrCat(lolo,"\pThe infection has been removed and the application repaired.");
  205.     Mention(lolo);
  206.     }
  207. if(check_only && i==1)
  208.     {
  209.     PStrCpy(lolo,"\pThe file '");
  210.     PStrCat(lolo,tana.name);
  211.     PStrCat(lolo,"\p' is infected with the 'Graphics Accelerator' virus, ");
  212.     PStrCat(lolo,"\pbut as it is locked cannot be repaired.");
  213.     Mention(lolo);
  214.     if(isapp) ExitToShell();
  215.     }
  216. if(!check_only && i==2)
  217.     {
  218.     PStrCpy(lolo,"\pThe file '");
  219.     PStrCat(lolo,tana.name);
  220.     PStrCat(lolo,"\p' is irretrievably damaged. It has been renamed '");
  221.     PStrCat(lolo,tana.name);
  222.     PStrCat(lolo,"\p.bad'. You should delete and reinstall it.");
  223.     Mention(lolo);
  224.     if(isapp) ExitToShell();
  225.     }
  226. if(check_only && i==2)
  227.     {    // yes, I know we can still rename if it's locked, but we don't
  228.     PStrCpy(lolo,"\pThe file '");
  229.     PStrCat(lolo,tana.name);
  230.     PStrCat(lolo,"\p' is irretrievably damaged. You should delete and reinstall it.");
  231.     Mention(lolo);
  232.     if(isapp) ExitToShell();
  233.     }
  234.  
  235. /* We make certain we're not going to call ExitToShell before there are processes! */
  236.  
  237. (*oldInitMenus)();
  238.  
  239. ExitCodeResource();
  240. }
  241.  
  242.  
  243. void PStrCpy(Str255 ct, ConstStr255Param cf)
  244. {
  245. BlockMove(cf,ct,cf[0]+1);
  246. }
  247.  
  248. void PStrCat(Str255 ct, ConstStr255Param cf)
  249. {
  250. BlockMove(&cf[1],&ct[ct[0]+1],cf[0]);
  251. ct[0]+=cf[0];
  252. }
  253.